package com.wanmait.wanmarecruit.webController;

import com.wanmait.wanmarecruit.entity.Company;
import com.wanmait.wanmarecruit.entity.Recruiter;
import com.wanmait.wanmarecruit.service.CompanyService;
import com.wanmait.wanmarecruit.service.RecruiterService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("web")
public class WebCompanySignUpController {

    @Resource
    private RecruiterService recruiterService;
    @Resource
    private CompanyService companyService;

    @RequestMapping("signUpRecruiter")
    public String signUpRecruiter(Recruiter recruiter){
        recruiter.setEnable(false);
        recruiterService.save(recruiter);
        return "redirect:/web/companies/companySignUpChooseCompanyOrNot?id="+recruiter.getId();
    }

    @RequestMapping("companySignUpVerify")
    @ResponseBody
    public String companySignUpVerify(String phone){
        boolean flag = recruiterService.verifyCompanySignUp(phone);
        if(!flag){
            return "false";
        }
        else {
            return "true";
        }
    }

    @PostMapping("signUpRecruitInfo")
    public String signUpRecruitInfo(Recruiter recruiter,String companyName)
    {
        String realCompanyName = companyName.substring(0,companyName.lastIndexOf(":")-3);
        String legalRepresentative = companyName.substring(companyName.lastIndexOf(":")+1,companyName.length()-1);
        Company company = companyService.getOneCompanyByNameAndLegal(realCompanyName,legalRepresentative);
        recruiter.setCompanyId(company.getId());
        recruiterService.updateNotEnableRecruitInfo(recruiter);
        return "redirect:/web/companies/companySignIn";
    }

    @RequestMapping("/companies/signUpOneCompany")
    public void signUpOneCompany()
    {

    }

    @PostMapping("/signUpOneCompanyInfo")
    public String signUpOneCompanyInfo(Company company)
    {
        company.setEnable(false);
        companyService.save(company);
        return "redirect:/web/companies/companySignIn?wait=1";
    }
}
